package scau.hotel.hotel_model.exception;


import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.authentication.BadCredentialsException;
import org.springframework.security.authentication.CredentialsExpiredException;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import scau.hotel.hotel_model.controller.RespBean;
import scau.hotel.hotel_model.controller.RespBeanEnum;

import java.sql.SQLIntegrityConstraintViolationException;


/*
*
* 全局异常处理类
 */
@RestControllerAdvice
public class GlobalExceptionHandler {


    /**
     * 捕获异常
     * @param e 异常
     * @return RespBean
     */
    @ExceptionHandler(Exception.class )
    public RespBean ExceptionHandler(Exception e){
        //捕获全局异常
        if(e instanceof GlobalException){
            GlobalException ex = (GlobalException) e;
            return RespBean.error((ex.getRespBeanEnum()));
        }
        //捕获MethodArgumentNotValidException
        else if(e instanceof MethodArgumentNotValidException){
            MethodArgumentNotValidException ex = (MethodArgumentNotValidException) e;
            RespBean respBean = RespBean.error(RespBeanEnum.BIND_ERROR);
            respBean.setMessage("参数校验异常："
//                    +ex.getBindingResult().getFieldError().getField() //获取格式出错的具体参数！！
                    +ex.getBindingResult().getAllErrors().get(0).getDefaultMessage());
            return respBean;
        }
        //捕获CustomException
        else if(e instanceof CustomException){
            CustomException ce = (CustomException) e;
            return  RespBean.error(ce.getRespBeanEnum(),ce.getMessage());
        }
        //捕获BadCredentialsException
        else if(e instanceof BadCredentialsException){
            return RespBean.error(RespBeanEnum.UNAUTHORIZED_ERROR,"用户名或者密码错误");//401
        }
        //捕获UsernameNotFoundException
        else if(e instanceof UsernameNotFoundException){
            return RespBean.error(RespBeanEnum.UNAUTHORIZED_ERROR,e.getMessage());//401
        }
        //捕获AccessDeniedException
        else if(e instanceof AccessDeniedException){
            return RespBean.error(RespBeanEnum.FORBIDDEN_ERROR,e.getMessage());//403
        }
        //捕获CredentialsExpiredException
        else if(e instanceof CredentialsExpiredException){
            return RespBean.error(RespBeanEnum.UNAUTHORIZED_ERROR,e.getMessage());//401
        }
        //捕获DataIntegrityViolationException异常,当插入、删除和修改数据的时候，违背的数据完整性约束抛出的异常
        else if(e instanceof DataIntegrityViolationException){
            return RespBean.error(RespBeanEnum.CONFLICT);//409
        }
        //捕获SQLIntegrityConstraintViolationException异常,要存入数据库表中的数据与已有的数据产生冲突,冲突来源于为字段赋予的主键,唯一等等
        else if(e instanceof SQLIntegrityConstraintViolationException){
            return RespBean.error(RespBeanEnum.CONFLICT);//409
        }
        //未捕获则打印异常
        System.out.println("异常处理器捕获到未识别异常：\n"+e+"\n");
        return RespBean.error(RespBeanEnum.ERROR);
    }
}
